//
//  DAnalysisDateRowsModel.swift
//  Drop
//
//  Created by hushaohua on 2018/5/25.
//  Copyright © 2018年 init. All rights reserved.
//

import Foundation

enum DAnalysisDatePickType{
    case none
    case everyXDay
    case calendar
}

class DAnalysisDatePrefItem : Equatable {
    static func == (lhs: DAnalysisDatePrefItem, rhs: DAnalysisDatePrefItem) -> Bool {
        return lhs.interval == rhs.interval && lhs.title == rhs.title;
    }
    
    var interval:HTimeIntervalType = .day;
    var title:String = "";
    var detail:String? = nil;
    var datePredicate:NSPredicate?;
    var closure:(() -> Void)?;
    
    func text() -> String{
        if (self.detail != nil){
            return detail!;
        }
        return self.title;
    }
    
    var datePickType:DAnalysisDatePickType = .none;
    var calenarPickType:HCalendarPickType = .unit;
    var isEveryWeekday:Bool = true;
    
    init(title:String) {
        self.title = title;
    }
    
    init(title:String, week:Bool) {
        self.title = title;
        self.datePickType = .everyXDay;
        self.isEveryWeekday = week;
    }
    
    init(title:String, calendarPick:HCalendarPickType) {
        self.title = title;
        self.datePickType = .calendar;
        self.calenarPickType = calendarPick;
    }
    
    func update(interval:HTimeIntervalType) -> DAnalysisDatePrefItem{
        self.interval = interval;
        return self;
    }
}

class DAnalysisDatePrefSection {
    var title:String = "";
    var items:[DAnalysisDatePrefItem]?;
    
    func addItem(_ item:DAnalysisDatePrefItem) -> Void {
        if (self.items == nil){
            self.items = [DAnalysisDatePrefItem]();
        }
        self.items!.append(item);
    }
}

class DAnalysisDateRowsModel {
    
    init() {
        self.currentSections = self.daySections;
    }
    
    func numberOfSection() -> Int{
        return self.currentSections != nil ? self.currentSections!.count : 0;
    }
    func rowsInSection(_ section:Int) -> Int{
        return self.currentSections![section].items!.count;
    }
    func sectionAt( _ section:Int) -> DAnalysisDatePrefSection{
        return self.currentSections![section];
    }
    
    var currentSections:[DAnalysisDatePrefSection]?;
    func setDayAsCurrent() -> Void {
        self.currentSections = self.daySections;
    }
    func setWeekAsCurrent() -> Void {
        self.currentSections = self.weekSections;
    }
    func setMonthAsCurrent() -> Void {
        self.currentSections = self.monthSections;
    }
    
    var currentItem:DAnalysisDatePrefItem?{
        didSet{
            oldValue?.detail = nil;
        }
    }
    func latestDayPrefItem(_ latestDay:Int) -> DAnalysisDatePrefItem {
        let pref = DAnalysisDatePrefItem(title: "最近\(latestDay)天");
        pref.interval = .day;
        let predicate:NSPredicate = NSPredicate(format: "(begin >= %@)", Date().startDayDate(after: -(latestDay - 1))! as NSDate);
        pref.datePredicate = predicate;
        return pref;
    }
    lazy var daySections:[DAnalysisDatePrefSection] = {
        var sections = [DAnalysisDatePrefSection]();
        
        let latest = DAnalysisDatePrefSection();
        latest.addItem(self.latestDayPrefItem(3));
        latest.addItem(self.latestDayPrefItem(7));
        latest.addItem(self.latestDayPrefItem(30));
        sections.append(latest);
        
        let period = DAnalysisDatePrefSection();
        period.addItem(DAnalysisDatePrefItem(title: "所有").update(interval: .day));
        period.addItem(DAnalysisDatePrefItem(title: "开始 - 结束", calendarPick:.continuous).update(interval: .day));
        sections.append(period);
        
        let day  = DAnalysisDatePrefSection();
        day.title = "";
        day.addItem(DAnalysisDatePrefItem(title: "某一天", calendarPick:.unit).update(interval: .day));
        day.addItem(DAnalysisDatePrefItem(title: "某些天", calendarPick:.discrete).update(interval: .day));
        sections.append(day);
        
        let advanced  = DAnalysisDatePrefSection();
        advanced.title = "";
        let perWeekday:DAnalysisDatePrefItem = DAnalysisDatePrefItem(title: "每周x");
        perWeekday.interval = .day;
        perWeekday.datePickType = .everyXDay;
        perWeekday.isEveryWeekday = true;
        advanced.addItem(perWeekday);
        
        let perDay:DAnalysisDatePrefItem = DAnalysisDatePrefItem(title: "每月xx日");
        perDay.datePickType = .everyXDay;
        perDay.interval = .day;
        perDay.isEveryWeekday = false;
        advanced.addItem(perDay);
        sections.append(advanced);
        
        return sections;
    }();
    
    func latestWeekPrefItem(_ latestWeeks:Int) -> DAnalysisDatePrefItem {
        let titleString = (latestWeeks == 1 ? "本周" : "最近\(latestWeeks)周");
        let pref = DAnalysisDatePrefItem(title: titleString);
        pref.interval = .week;
        let predicate:NSPredicate = NSPredicate(format: "(begin >= %@)", Date().firstWeekDay(after: -(latestWeeks - 1))! as NSDate);
        pref.datePredicate = predicate;
        return pref;
    }
    lazy var weekSections:[DAnalysisDatePrefSection] = {
        var sections = [DAnalysisDatePrefSection]();
        
        let latest = DAnalysisDatePrefSection();
        latest.addItem(self.latestWeekPrefItem(1));
        latest.addItem(self.latestWeekPrefItem(2));
        latest.addItem(self.latestWeekPrefItem(3));
        sections.append(latest);
        
        let period = DAnalysisDatePrefSection();
        period.addItem(DAnalysisDatePrefItem(title: "所有").update(interval: .week));
        period.addItem(DAnalysisDatePrefItem(title: "开始 - 结束", calendarPick:.continuous).update(interval: .week));
        sections.append(period);
        
        let week  = DAnalysisDatePrefSection();
        week.title = "";
        week.addItem(DAnalysisDatePrefItem(title: "某一周", calendarPick:.unit).update(interval: .week));
        week.addItem(DAnalysisDatePrefItem(title: "某些周", calendarPick:.discrete).update(interval: .week));
        sections.append(week);
        
        return sections;
    }();
    
    func latestMonthPrefItem(_ latestMonths:Int) -> DAnalysisDatePrefItem {
        let titleString = (latestMonths == 1 ? "本月" : "最近\(latestMonths)月");
        let pref = DAnalysisDatePrefItem(title: titleString);
        pref.interval = .month;
        let predicate:NSPredicate = NSPredicate(format: "(begin >= %@)", Date().firstMonthDay(after: -(latestMonths - 1))! as NSDate);
        pref.datePredicate = predicate;
        return pref;
    }
    lazy var monthSections:[DAnalysisDatePrefSection] = {
        var sections = [DAnalysisDatePrefSection]();
        
        let latest = DAnalysisDatePrefSection();
        latest.addItem(self.latestMonthPrefItem(1));
        latest.addItem(self.latestMonthPrefItem(2));
        sections.append(latest);
        
        let period = DAnalysisDatePrefSection();
        period.addItem(DAnalysisDatePrefItem(title: "所有").update(interval: .month));
        period.addItem(DAnalysisDatePrefItem(title: "开始 - 结束", calendarPick:.continuous).update(interval: .month));
        sections.append(period);
        
        let month  = DAnalysisDatePrefSection();
        month.title = "";
        month.addItem(DAnalysisDatePrefItem(title: "某一月", calendarPick:.unit).update(interval: .month));
        month.addItem(DAnalysisDatePrefItem(title: "某些月", calendarPick:.discrete).update(interval: .month));
        sections.append(month);
        
        return sections;
    }();
}
